// 详情展示配置文件

// 详情字段配置接口
export interface DetailFieldConfig {
  key: string; // 字段键名
  label: string; // 字段标签
  type: 'text' | 'date' | 'dateRange' | 'file' | 'custom'; // 字段类型
  required?: boolean; // 是否必填（用于显示样式）
  formatter?: (value: any, item?: any) => string; // 自定义格式化函数
  visible?: (item: any) => boolean; // 是否可见的条件函数
  defaultValue?: string; // 默认值（当值为空时显示）
  fileConfig?: {
    isMultiple?: boolean; // 是否多文件
    fileNamePrefix?: string; // 文件名前缀
  };
}

// 详情展示配置接口
export interface DetailDisplayConfig {
  title: string; // 模块标题
  icon: string; // 图标类名
  headerFields: DetailFieldConfig[]; // 头部显示的字段（标题行）
  detailFields: DetailFieldConfig[]; // 详情显示的字段
  emptyText: string; // 空状态文本
  emptyHint: string; // 空状态提示
  addButtonText: string; // 新增按钮文本
}

// 通用格式化函数
export const formatters = {
  // 日期格式化 - 使用统一的日期格式化逻辑
  date: (value: any) => {
    if (!value) return "-";

    let date: Date;

    // 如果已经是Date对象，直接使用
    if (value instanceof Date) {
      date = value;
    }
    // 如果是数字类型的时间戳，直接使用
    else if (typeof value === "number") {
      date = new Date(value);
    }
    // 如果是字符串，需要处理iOS兼容性
    else {
      const timeStr = value.toString();

      // 处理常见的时间格式，转换为iOS兼容格式
      let isoString = timeStr;

      // 如果是 "yyyy-MM-dd HH:mm:ss" 格式，转换为 "yyyy/MM/dd HH:mm:ss"
      if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(timeStr)) {
        isoString = timeStr.replace(/(\d{4})-(\d{2})-(\d{2})/, "$1/$2/$3");
      }
      // 如果是 "yyyy-MM-dd" 格式，转换为 "yyyy/MM/dd"
      else if (/^\d{4}-\d{2}-\d{2}$/.test(timeStr)) {
        isoString = timeStr.replace(/(\d{4})-(\d{2})-(\d{2})/, "$1/$2/$3");
      }
      // 如果是完整的GMT格式（如 "Tue Sep 16 2025 10:19:08 GMT+0800 (CST)"），直接使用
      // 这种格式通常是服务端返回的完整时间字符串，浏览器可以正确解析
      // 如果是ISO 8601格式（yyyy-MM-ddTHH:mm:ss），iOS原生支持，直接使用
      // 其他格式也直接尝试使用

      date = new Date(isoString);
    }

    // 检查日期是否有效
    if (isNaN(date.getTime())) {
      return "-";
    }

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const day = String(date.getDate()).padStart(2, "0");

    return `${year}-${month}-${day}`;
  },

  // 日期范围格式化
  dateRange: (startDate: any, endDate: any) => {
    const start = formatters.date(startDate);
    const end = endDate ? formatters.date(endDate) : "至今";
    if (start === "-") return "-";
    return `${start} - ${end}`;
  },

  // 默认文本格式化
  text: (value: any, defaultValue: string = "-") => {
    return value || defaultValue;
  },

  // 工作单位特殊格式化（处理多种可能的字段结构）
  workCompany: (value: any, item: any, defaultValue: string = "-") => {
    // 优先级：直接字段值 > Org.Name > Company > OrgName
    const companyName = value || item?.Org?.Name || item?.Company || item?.OrgName;
    return companyName || defaultValue;
  }
};

// 获取教育文件ID列表的辅助函数
function getEducationFileIds(fileIdsStr: string): string[] {
  if (!fileIdsStr) return [];
  return fileIdsStr.split(',').filter(id => id.trim());
}

// 学习经历详情展示配置
export const educationDisplayConfig: DetailDisplayConfig = {
  title: "学习经历",
  icon: "shuben",
  headerFields: [
    {
      key: "School",
      label: "学校名称",
      type: "text",
      formatter: (value) => formatters.text(value, "")
    },
    {
      key: "dateRange",
      label: "时间范围",
      type: "dateRange",
      formatter: (_value, item) => formatters.dateRange(item?.EnrollmentDate, item?.GraduationDate)
    }
  ],
  detailFields: [
    {
      key: "School",
      label: "学校",
      type: "text",
      formatter: (value) => formatters.text(value)
    },
    {
      key: "Major",
      label: "专业",
      type: "text",
      formatter: (value) => formatters.text(value)
    },
    {
      key: "EducationLevel",
      label: "学历",
      type: "text",
      formatter: (value) => formatters.text(value)
    },
    {
      key: "EnrollmentDate",
      label: "入学日期",
      type: "date",
      formatter: (value) => formatters.date(value)
    },
    {
      key: "GraduationDate",
      label: "毕业日期",
      type: "date",
      formatter: (value) => value ? formatters.date(value) : "在读"
    },
    {
      key: "EducationSystem",
      label: "学制",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.EducationSystem
    },
    {
      key: "SelectedEducationFilesIds",
      label: "学历证书",
      type: "file",
      formatter: (value) => value,
      visible: (item) => {
        const fileIds = getEducationFileIds(item.SelectedEducationFilesIds);
        return fileIds && fileIds.length > 0;
      },
      fileConfig: {
        isMultiple: true,
        fileNamePrefix: "学历证书"
      }
    }
  ],
  emptyText: "暂无学习经历记录",
  emptyHint: "该人员暂未录入学习经历信息",
  addButtonText: "新增学习经历"
};

// 家庭成员详情展示配置
export const familyDisplayConfig: DetailDisplayConfig = {
  title: "家庭成员",
  icon: "kehu-kehulianxiren",
  headerFields: [
    {
      key: "MemberName",
      label: "成员姓名",
      type: "text",
      formatter: (value) => formatters.text(value, "")
    },
    {
      key: "RelationshipType",
      label: "关系",
      type: "text",
      formatter: (value) => formatters.text(value, "")
    }
  ],
  detailFields: [
    {
      key: "MemberName",
      label: "姓名",
      type: "text",
      formatter: (value) => formatters.text(value)
    },
    {
      key: "RelationshipType",
      label: "关系",
      type: "text",
      formatter: (value) => formatters.text(value)
    },
    {
      key: "BirthDate",
      label: "出生日期",
      type: "date",
      formatter: (value) => formatters.date(value)
    },
    {
      key: "Ethnicity",
      label: "民族",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.Ethnicity
    },
    {
      key: "ContactPhone",
      label: "联系电话",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.ContactPhone
    },
    {
      key: "WorkOrSchool",
      label: "工作/学校",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.WorkOrSchool
    },
    {
      key: "JobOrGrade",
      label: "职务/年级",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.JobOrGrade
    }
  ],
  emptyText: "暂无家庭成员记录",
  emptyHint: "该人员暂未录入家庭成员信息",
  addButtonText: "新增家庭成员"
};

// 工作经历详情展示配置
export const workDisplayConfig: DetailDisplayConfig = {
  title: "工作经历",
  icon: "gongwenbao",
  headerFields: [
    {
      key: "Company",
      label: "公司名称",
      type: "text",
      formatter: (value, item) => formatters.workCompany(value, item, "")
    },
    {
      key: "dateRange",
      label: "时间范围",
      type: "dateRange",
      formatter: (_value, item) => formatters.dateRange(item?.StartDate, item?.EndDate)
    }
  ],
  detailFields: [
    {
      key: "Company",
      label: "工作单位",
      type: "text",
      formatter: (value, item) => formatters.workCompany(value, item)
    },
    {
      key: "Position",
      label: "岗位",
      type: "text",
      formatter: (value) => formatters.text(value)
    },
    {
      key: "StartDate",
      label: "到职日期",
      type: "date",
      formatter: (value) => formatters.date(value)
    },
    {
      key: "EndDate",
      label: "离职日期",
      type: "date",
      formatter: (value) => value ? formatters.date(value) : "至今"
    },
    {
      key: "Reference",
      label: "证明人",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.Reference
    }
  ],
  emptyText: "暂无工作经历记录",
  emptyHint: "该人员暂未录入工作经历信息",
  addButtonText: "新增工作经历"
};

// 荣誉职称详情展示配置
export const honorDisplayConfig: DetailDisplayConfig = {
  title: "荣誉职称",
  icon: "renshi-jiangchengjilu",
  headerFields: [
    {
      key: "QualificationName",
      label: "资质名称",
      type: "text",
      formatter: (value, item) => formatters.text(value || item?.TitleName, "资质名称")
    },
    {
      key: "QualificationLevel",
      label: "资质等级",
      type: "text",
      formatter: (value) => formatters.text(value)
    }
  ],
  detailFields: [
    {
      key: "QualificationName",
      label: "资质名称",
      type: "text",
      formatter: (value, item) => formatters.text(value || item?.TitleName)
    },
    {
      key: "ProfessionalTitleTypeName",
      label: "资质类别",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.ProfessionalTitleTypeName
    },
    {
      key: "QualificationLevel",
      label: "资质等级",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.QualificationLevel
    },
    {
      key: "IssuingAuthority",
      label: "发证单位",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.IssuingAuthority
    },
    {
      key: "CertificateCode",
      label: "证书编码",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.CertificateCode
    },
    {
      key: "ObtainedDate",
      label: "获得日期",
      type: "date",
      formatter: (value) => formatters.date(value),
      visible: (item) => !!item.ObtainedDate
    },
    {
      key: "ExpiryDate",
      label: "有效期日期",
      type: "date",
      formatter: (value) => formatters.date(value),
      visible: (item) => !!item.ExpiryDate
    },
    {
      key: "SalaryReward",
      label: "薪资奖励",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.SalaryReward
    },
    {
      key: "Remark",
      label: "备注",
      type: "text",
      formatter: (value) => formatters.text(value),
      visible: (item) => !!item.Remark
    }
  ],
  emptyText: "暂无荣誉职称记录",
  emptyHint: "该人员暂未录入荣誉职称信息",
  addButtonText: "新增荣誉职称"
};
